package com.hualian.dance.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.hualian.dance.service.DanceService;
import com.hualian.system.common.Constants;
import com.hualian.system.controller.BaseController;
import com.hualian.util.http.RequestUtil;

@RestController
@RequestMapping("/dance")
public class DanceController extends BaseController{
	
	@Autowired
	private DanceService danceService;
	

	/**
	 * 将参数中的非String类型字段进行处理
	 * @param params
	 * @return
	 */
	public Map<String, Object> convertParams(Map<String, Object> params) {
		// 如果开始时间为空,则设置为null
		if (params.get("beginDate") != null && params.get("beginDate").toString().trim() == "") {
			params.put("beginDate", null);
		}
		// 如果结束时间为空,则设置为null
		if (params.get("endDate") != null && params.get("endDate").toString().trim() == "") {
			params.put("endDate", null);
		}
		// 将期数转化为int类型
		int totalPhase = Integer.valueOf((params.get("totalPhase") == null || params.get("totalPhase").toString().trim() == "") ? "0" : params.get("totalPhase").toString().trim());
		params.put("totalPhase", totalPhase);

		return params;
	}
	
	
	
	/**
	 * 跳转到舞林大会首页
	 * 
	 * @return
	 */
	@RequestMapping("/toDanceIndex")
	public ModelAndView toDance() {
		return new ModelAndView("dance/danceIndex");
	}
	/**
	 * 跳转到报名页面
	 * @return
	 */
	@RequestMapping("/toSignUp")
	public ModelAndView toSignUp() {
		return new ModelAndView("dance/signUp");
	}
	
	/**
	 * 跳转到活动明细页面
	 * @return
	 */
	@RequestMapping("/toDanceAcDetail")
	public ModelAndView toDanceAcDetail() {
		return new ModelAndView("dance/danceAcDetail");
	}

	/**
	 * 跳转到活动报名页面
	 * @return
	 */
	@RequestMapping("/toAcApply")
	public ModelAndView toAcApply() {
		return new ModelAndView("dance/activityApply");
	}
	
	
	/**
	 * 查询活动
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("getActivity")
	public Map<String, Object> getActivity(HttpSession session, HttpServletRequest request) {
	
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		
		//参数
		Map<String, Object> params = RequestUtil.convertParamToMap(request);
		//easyui分页
		params = getPage(params);
		//查询活动
		List<Map<String, Object>>  rows = danceService.getActivity(params);
		//查询记录数
		int total = danceService.getActivityCount(params);
		
		jsonMap.put(TOTAL, total);
		jsonMap.put(ROWS, rows);
		
		return jsonMap;
	}
	
	
	/**
	 * 添加活动
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("addActivity")
	public Map<String, Object> addActivity(HttpSession session, HttpServletRequest request) {
	
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		
		//参数
		Map<String, Object> params = super.convertParamToMap(request);
		
		//将参数中的非String类型字段进行处理
		this.convertParams(params);
		
		try {
			int add = danceService.addActivity(params);

			if (add == 1) {
				// 新增成功
				jsonMap.put(SUCCESS, true);
			} else {
				// 新增失败
				jsonMap.put(SUCCESS, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonMap.put(SUCCESS, false);
		}
		
		return jsonMap;
	}

	/**
	 * 修改活动
	 */
	@RequestMapping("updateActivity")
	public Map<String, Object> updateActivity(HttpSession session, HttpServletRequest request) {
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		//参数
		Map<String, Object> params = super.convertParamToMap(request);
		
		//将参数中的非String类型字段进行处理
		this.convertParams(params);
		
		int update = danceService.updateActivity(params);
		
		if(update == 1){
			//修改成功
			jsonMap.put(SUCCESS, true);
		}else{
			//修改失败
			jsonMap.put(SUCCESS, false);
		}
		return jsonMap;
	}

	
	/**
	 * 删除活动
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("removeActivity")
	public Map<String, Object> removeActivity(int [] ids) {
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		int update = danceService.removeActivity(ids);
		
		if(update == ids.length){
			//修改成功
			jsonMap.put(SUCCESS, true);
		}else{
			//修改失败
			jsonMap.put(SUCCESS, false);
		}
		return jsonMap;
	}
	
	
	
	/**
	 * 查询活动明细
	 */
	@RequestMapping("getActivityDetail")
	public Map<String,Object> getActivityDetail(HttpSession session ,HttpServletRequest request){
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		//参数
		Map<String, Object> params = super.convertParamToMap(request);
		//easyui分页
		params = getPage(params);
		//查询活动明细
		List<Map<String, Object>>  rows = danceService.getActivityDetail(params);
		//查询记录数
		int total = danceService.getActivityDetailCount(params);
		
		jsonMap.put(TOTAL, total);
		jsonMap.put(ROWS, rows);
		
		return jsonMap;
	}
	
	
	
	/**
	 * 增加活动明细
	 */
	@RequestMapping("addActivityDetail")
	public Map<String, Object> addActivityDetail(HttpSession session, HttpServletRequest request) {
		
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		
		//参数
		Map<String, Object> params = super.convertParamToMap(request);
		
		//将参数中的非String类型字段进行处理
		this.convertParams(params);
		
		try {
			int add = danceService.addActivityDetail(params);

			if (add == 1) {
				// 新增成功
				jsonMap.put(SUCCESS, true);
			} else {
				// 新增失败
				jsonMap.put(SUCCESS, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonMap.put(SUCCESS, false);
		}
		
		return jsonMap;
	}
	/**
	 * 修改活动明细
	 */
	
	@RequestMapping("updateActivityDetail")
	public Map<String, Object> updateActivityDetail(HttpSession session, HttpServletRequest request) {
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		//参数
		Map<String, Object> params = super.convertParamToMap(request);
		
		//将参数中的非String类型字段进行处理
		this.convertParams(params);
		
		int update = danceService.updateActivityDetail(params);
		
		if(update == 1){
			//修改成功
			jsonMap.put(SUCCESS, true);
		}else{
			//修改失败
			jsonMap.put(SUCCESS, false);
		}
		return jsonMap;
	}
	/**
	 * 删除活动明细
	 * @param ids
	 * @return
	 */
	@RequestMapping("removeActivityDetail")
	public Map<String, Object> removeActivityDetail(int [] ids) {
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		int update = danceService.removeActivityDetail(ids);
		
		if(update == ids.length){
			//修改成功
			jsonMap.put(SUCCESS, true);
		}else{
			//修改失败
			jsonMap.put(SUCCESS, false);
		}
		return jsonMap;
	}
	
	
	
	
	
	
	
	
	/**
	 * 关闭活动
	 */
	@RequestMapping("closeActivity")
	public Map<String, Object> closeActivity(HttpSession session, HttpServletRequest request) {
		
		//TODO 关闭活动的同时还要关闭活动明细以及活动分组 投票等等.....##########################################
		return null;
	}

	/**
	 * 报名开始
	 */
	@RequestMapping("applyStart")
	public Map<String, Object> applyStart(HttpSession session, HttpServletRequest request) {
		return null;
	}

	/**
	 * 查询活动报名信息
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/getActivityApply")
	public Map<String, Object> getActivityApply(HttpSession session, HttpServletRequest request){
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		//参数
		Map<String, Object> params = super.convertParamToMap(request);
		//easyui分页
		params = getPage(params);
		//查询活动明细
		List<Map<String, Object>>  rows = danceService.getActivityApply(params);
		//查询记录数
		int total = danceService.getActivityApplyCount(params);
		
		jsonMap.put(TOTAL, total);
		jsonMap.put(ROWS, rows);
		
		return jsonMap;
	}
	/**
	 * 新增活动报名
	 */
	@RequestMapping("addActivityApply")
	public Map<String, Object> addActivityApply(HttpSession session, HttpServletRequest request) {
		//TODO 按队伍报名
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		//参数
		Map<String, Object> params = super.convertParamToMap(request);
		
		//将参数中的非String类型字段进行处理
		this.convertParams(params);
		int apply=danceService.addActivityApply(params);
		if(apply == 1){
			//报名成功
			jsonMap.put(SUCCESS, true);
		}else{
			//报名失败
			jsonMap.put(SUCCESS, false);
		}
		return jsonMap;
	}
	/**
	 * 编辑活动报名
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("updateAcApply")
	public Map<String, Object> updateAcApply(HttpSession session, HttpServletRequest request) {
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		//参数
		Map<String, Object> params = super.convertParamToMap(request);
		
		//将参数中的非String类型字段进行处理
		this.convertParams(params);
		
		int update = danceService.updateActivityApply(params);
		
		if(update == 1){
			//修改成功
			jsonMap.put(SUCCESS, true);
		}else{
			//修改失败
			jsonMap.put(SUCCESS, false);
		}
		return jsonMap;
	}
	/**
	 * 删除活动报名
	 * @param ids
	 * @return
	 */
	@RequestMapping("removeActivityApply")
	public Map<String, Object> removeActivityApply(int [] ids) {
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		int update = danceService.removeActivityApply(ids);
		
		if(update == ids.length){
			//修改成功
			jsonMap.put(SUCCESS, true);
		}else{
			//修改失败
			jsonMap.put(SUCCESS, false);
		}
		return jsonMap;
	}
	/**
	 * 审核报名
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("applyActivity")
	public Map<String, Object> applyActivity(int [] ids) {
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		int update = danceService.applyActivity(ids);
		
		if(update == ids.length){
			//修改成功
			jsonMap.put(SUCCESS, true);
		}else{
			//修改失败
			jsonMap.put(SUCCESS, false);
		}
		return jsonMap;
	}
	/**
	 * 驳回报名
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("rejectActivity")
	public Map<String, Object> rejectActivity(int [] ids) {
		Map<String,Object> jsonMap = new HashMap<String, Object>();
		int update = danceService.rejectActivity(ids);
		
		if(update == ids.length){
			//修改成功
			jsonMap.put(SUCCESS, true);
		}else{
			//修改失败
			jsonMap.put(SUCCESS, false);
		}
		return jsonMap;
	}

	/**
	 * 报名结束
	 */
	@RequestMapping("applyEnd")
	public Map<String, Object> applyEnd(HttpSession session, HttpServletRequest request) {
		return null;
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 开始分组
	 */
	@RequestMapping("group")
	public Map<String, Object> group(HttpSession session, HttpServletRequest request) {
		return null;
	}

	/**
	 * 生成队伍投票界面
	 */
	
	@RequestMapping("voteStart")
	public Map<String, Object> voteStart(HttpSession session, HttpServletRequest request) {
		return null;
	}

	/**
	 * 投票
	 */
	@RequestMapping("vote")
	public Map<String, Object> vote(HttpSession session, HttpServletRequest request) {
		return null;
	}

	/**
	 * 统计票数
	 */
	@RequestMapping("voteCount")
	public Map<String,Object> voteCount(HttpSession session ,HttpServletRequest request){
		return null;
	}
	
	/**
	 * 获得进入下一轮的队伍名称
	 */
	@RequestMapping("getAdvancingTeams")
	public Map<String,Object> getAdvancingTeams (HttpSession session ,HttpServletRequest request){
		return null;
	}
	
	
	
	
}
